<template>
  <div class="portal-layout">
    <!-- 顶部导航栏 -->
    <header class="portal-header" :class="{ 'with-shadow': showHeaderShadow }">
      <div class="container">
        <div class="header-content">
          <div class="logo">
            <router-link to="/portal">
              <span class="logo-icon"><el-icon><FirstAidKit /></el-icon></span>
              <span class="logo-text">智慧医疗服务平台</span>
            </router-link>
          </div>
          
          <nav class="main-nav">
            <ul class="nav-list">
              <li class="nav-item" :class="{ 'active': isActive('/portal') }">
                <router-link to="/portal">首页</router-link>
              </li>
              <li class="nav-item" :class="{ 'active': isActive('/portal/departments') }">
                <router-link to="/portal/departments">科室导航</router-link>
              </li>
              <li class="nav-item" :class="{ 'active': isActive('/portal/experts') }">
                <router-link to="/portal/experts">专家团队</router-link>
              </li>
              <li class="nav-item" :class="{ 'active': isActive('/portal/guide') }">
                <router-link to="/portal/guide">就医指南</router-link>
              </li>
              <li class="nav-item" :class="{ 'active': isActive('/portal/news') }">
                <router-link to="/portal/news">系统动态</router-link>
              </li>
              <li class="nav-item" :class="{ 'active': isActive('/portal/about') }">
                <router-link to="/portal/about">关于系统</router-link>
              </li>
              <li class="nav-item">
                <a href="#" @click.prevent="handleUserManagement">用户管理</a>
              </li>
            </ul>
          </nav>
          
          <div class="header-right">
            <!-- 搜索按钮 -->
            <div class="search-btn">
              <el-tooltip content="搜索" placement="bottom">
                <el-icon @click="showSearch = true"><Search /></el-icon>
              </el-tooltip>
            </div>
            
            <!-- 语言切换 -->
            <div class="lang-switch">
              <el-dropdown trigger="click" @command="handleLangChange">
                <span class="lang-text">
                  <el-icon><Connection /></el-icon>
                  <span>{{ currentLang === 'zh' ? '中文' : 'English' }}</span>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item command="zh" :disabled="currentLang === 'zh'">中文</el-dropdown-item>
                    <el-dropdown-item command="en" :disabled="currentLang === 'en'">English</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
            
            <!-- 用户信息 -->
            <div class="user-area" v-if="isLoggedIn">
              <el-dropdown trigger="click">
                <div class="user-info">
                  <span class="user-name">{{ userInfo.name || userInfo.username }}</span>
                  <el-avatar :size="32" :src="userInfo.avatar" />
                </div>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item @click="navigateTo('/portal/patient/profile')">
                      <el-icon><User /></el-icon> 个人中心
                    </el-dropdown-item>
                    <el-dropdown-item @click="navigateTo('/portal/patient/appointments')" v-role="'patient'">
                      <el-icon><Calendar /></el-icon> 我的预约
                    </el-dropdown-item>
                    <el-dropdown-item @click="navigateTo('/portal/patient/records')">
                      <el-icon><Notebook /></el-icon> 就诊记录
                    </el-dropdown-item>
                    <el-dropdown-item divided @click="handleLogout">
                      <el-icon><SwitchButton /></el-icon> 退出登录
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
            
            <!-- 登录/注册按钮 -->
            <div class="auth-buttons" v-else>
              <el-button class="login-btn" type="text" @click="navigateTo('/login')">登录</el-button>
              <el-button class="register-btn" type="primary" @click="navigateTo('/register')">注册</el-button>
            </div>
          </div>
        </div>
      </div>
    </header>

    <!-- 搜索抽屉 -->
    <el-drawer
      v-model="showSearch"
      title="站内搜索"
      direction="ttb"
      size="50%"
    >
      <div class="search-drawer">
        <el-input
          v-model="searchKeyword"
          placeholder="请输入搜索关键词"
          clearable
          class="search-input"
        >
          <template #append>
            <el-button type="primary" @click="performSearch">
              <el-icon><Search /></el-icon>
              搜索
            </el-button>
          </template>
        </el-input>
        
        <div class="search-categories">
          <h3 class="search-categories-title">热门搜索</h3>
          <div class="search-tags">
            <el-tag
              v-for="tag in hotSearchTags"
              :key="tag"
              effect="plain"
              @click="searchKeyword = tag; performSearch();"
            >
              {{ tag }}
            </el-tag>
          </div>
        </div>
      </div>
    </el-drawer>
    
    <!-- 主要内容 -->
    <main class="portal-main">
      <router-view v-slot="{ Component }">
        <transition name="fade" mode="out-in">
          <component :is="Component" />
        </transition>
      </router-view>
    </main>
    
    <!-- 页脚 -->
    <footer class="portal-footer">
      <div class="container">
        <div class="footer-content">
          <div class="footer-logo">
            <h2>智慧医疗服务平台</h2>
            <p>提供专业、便捷的医疗服务</p>
          </div>
          
          <div class="footer-links">
            <div class="footer-link-group">
              <h3>关于系统</h3>
              <ul>
                <li><a href="#" @click.prevent="navigateTo('/portal/about')">系统简介</a></li>
                <li><a href="#" @click.prevent="navigateTo('/portal/history')">发展历程</a></li>
                <li><a href="#" @click.prevent="navigateTo('/portal/honor')">荣誉资质</a></li>
              </ul>
            </div>
            
            <div class="footer-link-group">
              <h3>系统服务</h3>
              <ul>
                <li v-role="'patient'"><a href="#" @click.prevent="navigateTo('/portal/patient/appointment')">预约挂号</a></li>
                <li><a href="#" @click.prevent="navigateTo('/portal/guide')">系统使用指南</a></li>
                <li><a href="#" @click.prevent="navigateTo('/portal/faq')">常见问题</a></li>
              </ul>
            </div>
            
            <div class="footer-link-group">
              <h3>联系系统</h3>
              <ul>
                <li>地址：XX市XX区XX路XX号</li>
                <li>电话：123-456-7890</li>
                <li>邮箱：contact@hospital.com</li>
              </ul>
            </div>
          </div>
        </div>
        
        <div class="footer-bottom">
          <p>© 2024 智慧医疗服务平台 版权所有</p>
          <p>
            <a href="#">隐私政策</a> | 
            <a href="#">使用条款</a> | 
            <a href="#">网站地图</a>
          </p>
        </div>
      </div>
    </footer>
    
    <!-- 快速返回顶部 -->
    <el-backtop :right="20" :bottom="20"></el-backtop>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 搜索相关
const showSearch = ref(false)
const searchKeyword = ref('')
const hotSearchTags = ['新冠疫苗', '体检套餐', '心脏科', '皮肤科', '预约挂号', '就医流程']

// 语言切换
const currentLang = ref('zh')

// 检查用户是否已登录
const isLoggedIn = computed(() => userStore.isLoggedIn)
const userInfo = computed(() => userStore.userInfo)

// 展开/收起移动端菜单
const mobileMenuOpen = ref(false)

// 页面滚动后显示阴影
const showHeaderShadow = ref(false)

// 判断路由是否激活
const isActive = (path) => {
  return route.path === path || route.path.startsWith(path + '/')
}

// 处理语言切换
const handleLangChange = (lang) => {
  currentLang.value = lang
  // 实际项目中可能需要调用i18n库来切换语言
}

// 导航到指定路径
const navigateTo = (path) => {
  router.push(path)
  mobileMenuOpen.value = false
}

// 执行搜索
const performSearch = () => {
  if (searchKeyword.value.trim()) {
    // 实际项目中应该导航到搜索结果页面
    console.log('搜索关键词:', searchKeyword.value)
    showSearch.value = false
  }
}

// 用户退出登录
const handleLogout = async () => {
  try {
    await userStore.logout()
    router.push('/login')
  } catch (error) {
    console.error('退出登录失败:', error)
  }
}

// 监听页面滚动
window.addEventListener('scroll', () => {
  showHeaderShadow.value = window.scrollY > 10
})

// 切换移动菜单状态
const toggleMobileMenu = () => {
  mobileMenuOpen.value = !mobileMenuOpen.value
}

// 处理用户管理跳转
const handleUserManagement = () => {
  if (!isLoggedIn.value) {
    // 未登录用户先导航到登录页面，并传递想要访问的路径
    router.push({ 
      path: '/portal/login', 
      query: { redirect: '/system/users' } 
    })
    ElMessage.info('请先登录系统')
  } else {
    // 已登录用户根据角色跳转
    const role = userStore.userRole
    if (role === 'admin') {
      // 管理员直接进入管理后台的用户管理页面
      router.push('/system/users')
    } else if (role === 'doctor') {
      // 医生进入医生工作站
      router.push('/portal/doctor/workstation')
    } else if (role === 'patient') {
      // 患者跳转到个人中心
      router.push('/portal/patient/profile')
    } else {
      // 其他角色跳转到个人中心
      router.push('/portal/user/profile')
      ElMessage.info('已为您导航到个人中心')
    }
  }
}
</script>

<style scoped>
.portal-layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* 头部样式 */
.portal-header {
  background-color: white;
  position: sticky;
  top: 0;
  z-index: 1000;
  border-bottom: 1px solid #ebeef5;
  height: 60px;
}

.portal-header.with-shadow {
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.header-content {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  display: flex;
  align-items: center;
  height: 60px;
}

.logo a {
  display: flex;
  align-items: center;
  text-decoration: none;
  height: 100%;
}

.logo-icon {
  font-size: 22px;
  color: #1989fa;
  margin-right: 8px;
  display: flex;
  align-items: center;
}

.logo-text {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  display: flex;
  align-items: center;
}

.main-nav {
  flex: 1;
  margin: 0 30px;
}

.nav-list {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  height: 60px;
}

.nav-item {
  margin: 0 12px;
  position: relative;
  height: 100%;
  display: flex;
  align-items: center;
}

.nav-item a {
  text-decoration: none;
  color: #333;
  font-size: 15px;
  display: block;
  padding: 0 5px;
  line-height: 60px;
  transition: color 0.3s;
}

.nav-item a:hover {
  color: #1989fa;
}

.nav-item.active a {
  color: #1989fa;
  font-weight: 500;
}

.nav-item.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #1989fa;
}

.header-right {
  display: flex;
  align-items: center;
  height: 60px;
}

.search-btn, .lang-switch {
  margin-right: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  height: 100%;
}

.search-btn .el-icon, 
.lang-switch .el-icon {
  font-size: 18px;
  color: #666;
  display: flex;
  align-items: center;
}

.user-area {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  height: 100%;
}

.user-name {
  margin-right: 8px;
  color: #333;
  font-size: 14px;
}

.auth-buttons {
  display: flex;
  align-items: center;
  height: 100%;
}

.login-btn {
  margin-right: 10px;
  height: 32px;
  display: flex;
  align-items: center;
  font-size: 14px;
}

.register-btn {
  padding: 6px 15px;
  font-size: 14px;
  height: 32px;
  display: flex;
  align-items: center;
}

/* 搜索抽屉 */
.search-drawer {
  padding: 20px;
}

.search-input {
  width: 100%;
}

.search-categories {
  margin-top: 30px;
}

.search-categories-title {
  font-size: 16px;
  color: #333;
  margin-bottom: 15px;
}

.search-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.search-tags .el-tag {
  cursor: pointer;
}

/* 主要内容 */
.portal-main {
  flex: 1;
}

/* 页脚样式 */
.portal-footer {
  background: #304156;
  color: white;
  padding: 60px 0 20px;
  margin-top: 40px;
}

.footer-content {
  display: flex;
  margin-bottom: 40px;
}

.footer-logo {
  flex: 1;
}

.footer-logo h2 {
  margin: 0 0 10px;
  font-size: 24px;
}

.footer-logo p {
  margin: 0;
  color: #ccc;
}

.footer-links {
  flex: 2;
  display: flex;
  justify-content: space-around;
}

.footer-link-group h3 {
  font-size: 18px;
  margin: 0 0 20px;
  position: relative;
  padding-bottom: 10px;
}

.footer-link-group h3::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 30px;
  height: 2px;
  background-color: #1989fa;
}

.footer-link-group ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.footer-link-group li {
  margin-bottom: 10px;
}

.footer-link-group a {
  color: #ddd;
  text-decoration: none;
  transition: color 0.3s;
}

.footer-link-group a:hover {
  color: #1989fa;
}

.footer-bottom {
  border-top: 1px solid #3f536e;
  padding-top: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #9aa9c1;
}

.footer-bottom a {
  color: #9aa9c1;
  text-decoration: none;
  transition: color 0.3s;
}

.footer-bottom a:hover {
  color: #1989fa;
}

/* 动画效果 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .main-nav {
    display: none; /* 在移动端需要使用汉堡菜单 */
  }
  
  .footer-content {
    flex-direction: column;
    gap: 30px;
  }
}

@media (max-width: 768px) {
  .footer-links {
    flex-direction: column;
    gap: 30px;
  }
  
  .footer-bottom {
    flex-direction: column;
    text-align: center;
    gap: 10px;
  }
}

.lang-text {
  display: flex;
  align-items: center;
  color: #666;
  height: 100%;
}

.lang-text .el-icon {
  margin-right: 5px;
  display: flex;
  align-items: center;
}
</style> 